package com.restphp.java.hashdb.dcs.client;

import com.alibaba.fastjson.JSONObject;
import com.restphp.java.hashdb.dcs.constant.IPType;
import com.restphp.java.hashdb.dcs.constant.MessageType;
import com.restphp.java.hashdb.dcs.po.ClusterNode;
import com.restphp.java.hashdb.dcs.po.Message;
import com.restphp.java.hashdb.dcs.utils.ClusterNodeUtils;

import java.io.*;
import java.net.Socket;

/**
 * 群集客户端.
 * Created by sofical on 2020/8/11.
 */
public class HashDbClusterClient {
    /**
     * 从指定群集节点中获取缓存值.
     * @param node 节点信息.
     * @param root 数据落地文件址。
     * @param table  表.
     * @param key 键名.
     * @param tClass 获取类型.
     * @param <T> T.
     * @return  结果.
     */
    public <T> T get(ClusterNode node, String root, String table, String key, Class<T> tClass) {
        Message message = new Message();
        message.setMessageType(MessageType.GET);
        message.setRoot(root);
        message.setTable(table);
        message.setKey(key);

        String info = this.client(node, message);

        return JSONObject.parseObject(info, tClass);
    }

    /**
     * 通知节点设置缓存.
     * @param node 节点.
     * @param root 数据落地文件址.
     * @param table 表.
     * @param key 键名.
     * @param value 值.
     * @param timeout 超时时间.
     * @param time 设置时间.
     */
    public void set (ClusterNode node, String root, String table, String key, Object value, Integer timeout, Long time) {
        Message message = new Message();
        message.setMessageType(MessageType.SET);
        message.setRoot(root);
        message.setTable(table);
        message.setKey(key);
        message.setValue(value);
        message.setTimeout(timeout);
        message.setTime(time);

        this.client(node, message);
    }

    /**
     * 删除键值.
     * @param node 节点.
     * @param root 数据落地文件址.
     * @param table 表.
     * @param key 键名.
     * @param time 设置时间.
     */
    public void delete(ClusterNode node, String root, String table, String key, Long time) {
        Message message = new Message();
        message.setMessageType(MessageType.DELETE);
        message.setRoot(root);
        message.setTable(table);
        message.setKey(key);
        message.setTime(time);

        this.client(node, message);
    }

    /**
     * 通知节点.
     * @param node 节点配置.
     * @param message 消息.
     * @return 通信结果.
     */
    private String client(ClusterNode node, Message message) {
        String ip = ClusterNodeUtils.getHostAddress(node);

        Socket socket = null;
        PrintStream ps = null;
        BufferedReader br = null;

        try {
            socket = new Socket(ip, node.getPort());
            ps = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // 发送消息
            ps.println(JSONObject.toJSONString(message));
            ps.flush();

            //接收返回消息
            String info = br.readLine();

            return info;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ps) {
                ps.close();
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    /*public static void main(String[] args) {
        ClusterNode node = new ClusterNode();
        String ip = "192.168.8.116";
        node.setIpType(IPType.IPV4);
        node.setAddress(ip);
        node.setPort(889);

        HashDbClusterClient clusterClient = new HashDbClusterClient();
        clusterClient.get(node, "D:\\var", "user", "user1", String.class);
    }*/
}
